﻿using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace SchemeManager.Crypt
{
    /// <summary>
    /// 方案文件加载器
    /// </summary>
    public class SchemeCrypt : SmartMeterParamFrame.Interface.ICrypt
    {
        public static SchemeCrypt Instance = null;
        /// <summary>
        /// 加密/解密KEY
        /// </summary>
        private string encryptKey = "ancestorseightgenerationsofyours";
        private string M_sIV = "HrxSqiWlb+A=";//矢量

        static SchemeCrypt()
        {
            Instance = new SchemeCrypt();
        }

        public SchemeCrypt()
        {
            if (Instance != null)
            {
                throw new Exception("单例模式构造，请不要重复实例化对象");
            }
        }


        /// <summary>
        /// 数据加密
        /// </summary>
        /// <param name="sourceData">要加密的字符串</param>
        /// <returns></returns>
        public byte[] Encrypt(byte[] sourceData)
        {

            //构造一个对称算法
            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
            //string M_sKey = "JEFHTWkrTAUXpfO8NJWXdGJIoQz0/Jfw";//密钥
            ICryptoTransform ct;//
            MemoryStream ms = new MemoryStream(); ;
            CryptoStream cs;
            byte[] byt = sourceData;//Encoding.UTF8.GetBytes(M_Value);
            mCSP.Key =Encoding.ASCII.GetBytes(encryptKey.Substring(0,24)) ;//Convert.FromBase64String(encryptKey);
            mCSP.IV = Encoding.ASCII.GetBytes(M_sIV.Substring(0,8));//Convert.FromBase64String(M_sIV);
            //指定加密的运算模式
            mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
            //获取或设置加密算法的填充模式
            mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return ms.ToArray();
        }

        /// <summary>
        /// 数据解密
        /// </summary>
        /// <param name="sourceData">要解密的字符串</param>
        /// <returns></returns>
        public byte[] Decrypt(byte[] sourceData)
        {

            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
            //string M_sKey = "JEFHTWkrTAUXpfO8NJWXdGJIoQz0/Jfw";//密钥
            //string M_sIV = "HrxSqiWlb+A=";//矢量
            ICryptoTransform ct;
            MemoryStream ms = new MemoryStream(); ;
            CryptoStream cs;
            byte[] byt = sourceData;
            mCSP.Key = Encoding.ASCII.GetBytes(encryptKey.Substring(0, 24));//Convert.FromBase64String(encryptKey);
            mCSP.IV = Encoding.ASCII.GetBytes(M_sIV.Substring(0, 8));//Convert.FromBase64String(M_sIV);
 
            mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
            mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return ms.ToArray();
        }
    }
}
